<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>继承</title>
</head>
<body>
<script>
    function Person(name, age) {
        this.name = name;
        this.age = age;
    }

    Person.prototype.eat = function () {
        console.log(`${this.age}岁的${this.name}吃饭`);
    };

    function Student(name, age) {
        Person.apply(this, [name, age]);
    }

    // 方式1-1 原型链继承
    // Student.prototype = Person.prototype;
    // Student.prototype.constructor = Student;
    // Student.prototype.name = "zhangsan";
    // 缺点：对子对象属性的改变，父对象的属性也会跟着改变

    // 1-2 2-1 2-2的原理都一样

    // 方式1-2 原型链继承
    // Student.prototype = new Person();
    // Student.prototype.constructor = Student;

    // 方式2-1 寄生组合继承 利用一个中间json，防止子对父的影响
    // var proxy = {};
    // Student.prototype = proxy;
    // proxy.__proto__ = Person.prototype;
    // proxy.constructor = Student;

    // 方式2-2 寄生组合继承，和上面的原理一样
    Student.prototype = Object.create(Person.prototype);
    Student.prototype.constructor = Student;

    // 所以Object.create的简单实现如下
    function create(prototype) {
        let obj = {};
        obj.__proto__ = prototype;
        return obj;
    }
</script>
</body>
</html>
